const bcrypt = require('bcryptjs')
const { query } = require('../config/database')
const config = require('../config')

async function createTestUser () {
  try {
    console.log('🔧 开始创建测试用户...')

    // 检查是否已存在测试用户
    const existingUsers = await query('SELECT id FROM users WHERE email = ?', [
      'test@example.com'
    ])

    if (existingUsers.length > 0) {
      console.log('✅ 测试用户已存在')
      return
    }

    // 加密密码
    const hashedPassword = await bcrypt.hash(
      '123456',
      config.security.bcryptSaltRounds
    )

    // 创建测试用户
    const result = await query(
      'INSERT INTO users (email, username, password_hash, nickname, coin_balance) VALUES (?, ?, ?, ?, ?)',
      ['test@example.com', 'testuser', hashedPassword, '测试用户', 1000]
    )

    console.log('✅ 测试用户创建成功:', {
      id: result.insertId,
      email: 'test@example.com',
      username: 'testuser',
      password: '123456'
    })
  } catch (error) {
    console.error('❌ 创建测试用户失败:', error)
  }

  process.exit(0)
}

createTestUser()
